<?php

class PackagingController extends BaseController {

	//packaging
	public function getPackaging()
	{
		Session::forget('list');
		Session::forget('filter');
		Session::forget('filterby');
		Session::forget('keyword');
		$packagings = DB::table('ref_packaging')
							->join('ref_papertype','papertype_id','=','packaging_papertype_id')
							->where('packaging_isdelete','=',0)
							->paginate(10);
		$papers = DB::table('ref_papertype')
							->where('papertype_type','=','packaging')
							->get();
		return View::make('admin.packaging.packaging',compact('packagings',$packagings,'papers',$papers));
	}

	public function postPackaging()
	{
		if(Input::get('filter')=='type'){
			Session::forget('name');
			Session::put('filter','type');
			Session::put('list', Input::get('typeby'));
			if(Input::get('typeby')=='all'){
				$packagings = DB::table('ref_packaging')
				->join('ref_papertype','papertype_id','=','packaging_papertype_id')
				->where('packaging_isdelete','=',0)
				->paginate(10);
			}else{
				$packagings = DB::table('ref_packaging')
				->join('ref_papertype','papertype_id','=','packaging_papertype_id')
				->where('packaging_isdelete','=',0)
				->where('packaging_type','=',Input::get('typeby'))
				->paginate(10);
			}
		}else if(Input::get('filter')=='keyword'){
				Session::forget('list');
				Session::put('filter','keyword');
				Session::put('name',Input::get('name'));
			$packagings = DB::table('ref_packaging')
			->join('ref_papertype','papertype_id','=','packaging_papertype_id')
			->where('packaging_isdelete','=',0)
			->where('papertype_name','LIKE','%'.Input::get('name').'%')
			->paginate(10);	
		}else{
			if(Session::get('filter')=="type"){
					if(Session::get('list')=='all'){
						$packagings = DB::table('ref_packaging')
						->join('ref_papertype','papertype_id','=','packaging_papertype_id')
						->where('packaging_isdelete','=',0)
						->paginate(10);
					}else{
						$packagings = DB::table('ref_packaging')
						->join('ref_papertype','papertype_id','=','packaging_papertype_id')
						->where('packaging_isdelete','=',0)
						->where('packaging_type','=',Session::get('list'))
						->paginate(10);
					}
			}else if(Session::get('filter')=="keyword"){
				$packagings = DB::table('ref_packaging')
						->join('ref_papertype','papertype_id','=','packaging_papertype_id')
						->where('packaging_isdelete','=',0)
						->where('papertype_name','LIKE','%'.Session::get('name').'%')
						->paginate(10);	
			}
		}
		$papers = DB::table('ref_papertype')
							->where('papertype_type','=','packaging')
							->get();
		return View::make('admin.packaging.packaging')->with('packagings',$packagings,'papers',$papers);
	}

	public function postAddpackaging()
	{
		$addpackaging = refpackaging::create(Input::all());

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Add data packaging";	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/packaging')->with('message','Datapackaging berhasil ditambahkan');
	}

	public function postEditpackaging($id)
	{
		$editpackaging = refpackaging::find($id);
		$editpackaging->packaging_type = Input::get('packaging_type');
		$editpackaging->packaging_papertype_id = Input::get('packaging_papertype_id');
		$editpackaging->packaging_price = Input::get('packaging_price');
		$editpackaging->packaging_length = Input::get('packaging_length');
		$editpackaging->packaging_width = Input::get('packaging_width');
		$editpackaging->packaging_height = Input::get('packaging_height');
		$editpackaging->packaging_qty_price = Input::get('packaging_qty_price');
		$editpackaging->packaging_qty_unit = Input::get('packaging_qty_unit');
		$editpackaging->save();

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Edit data packaging ".$id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		return Redirect::to('/packaging')->with('message','Data packaging berhasil diubahn');
	}

	public function postDeletepackaging()
	{
		$packaging = DB::table('ref_packaging')
				->get();
		foreach($packaging as $packaging){
		if(Input::get($packaging->packaging_id)=='1'){
			$id = Input::get($packaging->packaging_id);
			$packaging = DB::table('ref_packaging')
					->where('packaging_id','=',$packaging->packaging_id)
					->update(array('packaging_isdelete' => 1));

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Delete data packaging ".$packaging->packaging_id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		}
		}
		return Redirect::to('/packaging')->with('message','Data packaging berhasil dihapus');
	}



}